<template>
  <el-dialog
    title="新增员工"
    :visible="visible"
    width="30%"
    @close="closePop"
  >
    <el-form ref="form" label-width="120px" :model="form" :rules="rules">
      <el-form-item prop="username" label="姓名">
        <el-input v-model="form.username" style="width:80%" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item prop="mobile" label="手机">
        <el-input v-model="form.mobile" style="width:80%" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item prop="timeOfEntry" label="入职时间">
        <el-date-picker v-model="form.timeOfEntry" style="width:80%" placeholder="请选择入职时间" />
      </el-form-item>
      <el-form-item prop="formOfEmployment" label="聘用形式">
        <el-select v-model="form.formOfEmployment" style="width:80%" placeholder="请选择">
          <el-option
            v-for="item in hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="workNumber" label="工号">
        <el-input v-model="form.workNumber" style="width:80%" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item prop="departmentName" label="部门">
        <el-input v-model="form.departmentName" style="width:80%" placeholder="请选择部门" @focus="onDepartmentShow" />
        <el-tree v-if="isshow" v-loading="loading" :props="{label:'name'}" :data="treeData" @node-click="clicktree" />
      </el-form-item>
      <el-form-item prop="correctionTime" label="转正时间">
        <el-date-picker v-model="form.correctionTime" style="width:80%" placeholder="请选择转正时间" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="$emit('update:visible', false)">取 消</el-button>
      <el-button type="primary" @click="onconfirm">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getdepartments } from '@/api/departments'
import { transListToTree } from '@/utils/index'
import hireType from '@/api/constant/employees'
import { addEmployee } from '@/api/employee'
export default {
  props: {
    visible: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      form: {
        correctionTime: '',
        timeOfEntry: '',
        departmentName: '',
        workNumber: '',
        formOfEmployment: '',
        mobile: '',
        username: ''
      },
      rules: {
        username: [{ required: true, message: '用户姓名不能为空', trigger: 'blur' }, {
          min: 1, max: 4, message: '用户姓名为1-4位'
        }],
        mobile: [{ required: true, message: '手机号不能为空', trigger: 'blur' }, {
          pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur'
        }],
        formOfEmployment: [{ required: true, message: '聘用形式不能为空', trigger: 'change' }],
        workNumber: [{ required: true, message: '工号不能为空', trigger: 'blur' }],
        departmentName: [{ required: true, message: '部门不能为空', trigger: ['change', 'blur'] }],
        timeOfEntry: [{ required: true, message: '入职时间', trigger: 'blur' }],
        correctionTime: [{ required: true, message: '入职时间', trigger: 'blur' }]
      },
      treeData: [{
        label: '一级 1',
        children: [{
          label: '二级 1-1',
          children: [{
            label: '三级 1-1-1'
          }]
        }]
      }, {
        label: '一级 2',
        children: [{
          label: '二级 2-1',
          children: [{
            label: '三级 2-1-1'
          }]
        }, {
          label: '二级 2-2',
          children: [{
            label: '三级 2-2-1'
          }]
        }]
      }, {
        label: '一级 3',
        children: [{
          label: '二级 3-1',
          children: [{
            label: '三级 3-1-1'
          }]
        }, {
          label: '二级 3-2',
          children: [{
            label: '三级 3-2-1'
          }]
        }]
      }],
      isshow: false,
      loading: false,
      hireType: hireType.hireType
    }
  },

  created() {

  },

  methods: {
    closePop() {
      this.$emit('update:visible', false)
      this.$refs.form.resetFields()
      this.isshow = false
    },
    onDepartmentShow() {
      this.isshow = true
      this.loaddepartment()
    },
    async loaddepartment() {
      this.loading = true
      const res = await getdepartments()
      this.treeData = transListToTree(res.depts, '')
      this.loading = false
    },
    clicktree(data) {
      console.log(data)
      this.form.departmentName = data.name
      this.isshow = false
    },
    async onconfirm() {
      try {
        await this.$refs.form.validate()
        await addEmployee(this.form)
        this.$emit('update:visible', false)
        this.$emit('on-success')
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style scoped lang='less'>

</style>
